package org.osami.backoffice.eventswitch;

import sun.awt.image.OffScreenSurfaceManager;

import javax.annotation.Resource;
import javax.jms.*;
import javax.net.SocketFactory;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 22/09/11
 * Time: 11:40
 * To change this template use File | Settings | File Templates.
 */
public class TcpMain {

    public static final String HEADER_V1 = "mbeancmd_values_v1";
    public static final String HEADER_COLUMN_NAMES = "time;domain;server;sname;mbean;";
    public static final String HEADER_DATA_COLUMN_NAMES = "location;senseId;type;value;";

    public static void main(String[] args) throws Exception {

        Socket socket = SocketFactory.getDefault().createSocket("localhost", 18564);
        OutputStream os = socket.getOutputStream();

        StringBuilder header = new StringBuilder();
        header.append(HEADER_V1);
        header.append('\n');
        header.append(HEADER_COLUMN_NAMES);
        header.append(HEADER_DATA_COLUMN_NAMES);
        header.append('\n');

        double value = 0;
        while (true) {

            StringBuilder message = new StringBuilder();
            message.append(header);

            writeLine(message,
                      String.valueOf(System.currentTimeMillis()), // time   -> timestamp
                      "osami",                                    // domain -> n/a
                      "gateway",                                  // server -> n/a
                      "gateway",                                  // sname  -> n/a
                      "sense-1234",                               // mbean  -> sense.id
                    // extra attributes / event details or probes
                      "home",                                     // location
                      "sense-1234",                               // senseId
                      "luminosity",                               // type
                      String.valueOf(value++)                     // value
                      );

            os.write(message.toString().getBytes());
            os.flush();


            // separate each value in the time by x second
            Thread.sleep(2000);

            System.out.print(".");
        }
    }

    private static void writeLine(StringBuilder message, String... values) {

        boolean first = true;

        for (String value : values) {
            if (!first) {
                message.append(";");
            } else {
                first = false;
            }

            message.append(value);
        }

        message.append('\n');
    }

    private static HashMap<String, Serializable> createAlarm(String message) {
        HashMap<String, Serializable> data = new HashMap<String, Serializable>();

        data.put("alarm.id", "there-can-be-only-one");
        data.put("alarm.date", new Date(System.currentTimeMillis()));
        data.put("alarm.message", message);
        data.put("alarm.severity", "warning");

        data.put("source", "127.0.0.1");
        data.put("type", "luminosity");
        data.put("threshold", Double.valueOf(100));
        data.put("value", Double.valueOf(125));
        return data;
    }
}
